package in.muton.edushare.pages.cp.semCP;

import in.muton.edushare.components.messaging.api.UserMessage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.AcademicYearModelDAO;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ChangeSemesterPanel extends EdusharePanel implements IHeaderContributor {

	private static final long serialVersionUID = 4206748732782415037L;
	private IModel<Universities> univModel;
	private static Log log = LogFactory.getLog(ChangeSemesterPanel.class);
	Form<Void> f;
	WebMarkupContainer confirmDialogue;
	
	public ChangeSemesterPanel(String id, IModel<Universities> model, EduShareBasePage page) {
		super(id, model, page);
		this.univModel = model;
		
		//Display semester change block, if it's possible
		WebMarkupContainer wmc = new WebMarkupContainer("semChangeForm"){
			
			private static final long serialVersionUID = -3244329416824586472L;

			@Override
			public boolean isVisible() {
				return ChangeSemesterPanel.this.univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_CLOSE_ELECTIVE_SELECTION;
			}
		};
		add(wmc);
		wmc.add(new Link<Void>("rnCoursesLnk"){

			private static final long serialVersionUID = 130407899445667390L;

			@Override
			public void onClick() {
				((SemesterCP)getBasePage()).jumpTo(1);
			}
			
		});
		
		f = new Form<Void>("csf"){
			
			private static final long serialVersionUID = -4178864132218772033L;

			@Override
			protected void onSubmit() {
				IModel<Universities> currModel = ChangeSemesterPanel.this.univModel;
				currModel.getObject().setElectiveSelectionTime(Universities.UNIV_SCHEDULE_SEMESTER_CHANGE);
				String semName = new AcademicYearModelDAO(
												getORMInterface(), 
												currModel.getObject()
												)
												.getActiveSemester()
												.getSemesterName();
				
				String message = "The semester "
						+ semName
						+ ", "
						+ currModel.getObject().getCurrentAcadYear()
						+ " will be closed tonight."
						+ " Students will be assigned to their courses for the next semester and will be able to choose electives at that time.";
				CayenneList<Users> userList = new UnivDataHandler(currModel)
													.getEveryOne();

				log.debug("About to send message \n" + message);
				new UserMessage(message)
						.setReciever(userList)
						.setReciever(currModel.getObject())
						.setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
						.send();
				
				getORMInterface().commitAllChanges();
			}
		};
		wmc.add(f);
		
		AcademicYearModelDAO acadMDAO = new AcademicYearModelDAO(getORMInterface(), univModel.getObject());
		String currSem = acadMDAO.getSemesterName(acadMDAO.getActiveSemester().getSequenceNo(), univModel.getObject().getCurrentAcadYear());
		String nextSem = acadMDAO.getNextSemAsString();
		f.add(new Label("currSem",currSem));
		f.add(new Label("nextSem", nextSem));
		f.add(new Label("nextSemBut", nextSem));
		
		//confirmation dialogues
		confirmDialogue = new WebMarkupContainer("warnUser");
		confirmDialogue.add(new Label("dlgCurrSem", currSem));
		confirmDialogue.add(new Label("dlgNextSem", nextSem));
		confirmDialogue.setOutputMarkupId(true);
		f.add(confirmDialogue);
		
		//If it's elective selection zone, show warning block
		WebMarkupContainer wmcElective = new WebMarkupContainer("goToAllotElective"){
			
			private static final long serialVersionUID = 1527554173280478999L;

			@Override
			public boolean isVisible() {
				return ChangeSemesterPanel.this.univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_ALLOW_ELECTIVE_SELECTION;
			}
		};
		add(wmcElective);
		wmcElective.add(new Link<Void>("eallot1"){
			private static final long serialVersionUID = -7202282206034640576L;

			@Override
			public void onClick() {
				((SemesterCP)getBasePage()).jumpTo(2);
			}
		});
		wmcElective.add(new Link<Void>("eallot2"){
			private static final long serialVersionUID = -8926059067578377003L;

			@Override
			public void onClick() {
				((SemesterCP)getBasePage()).jumpTo(2);
			}
		});
		wmcElective.add(new Label("acsem",
								new AcademicYearModelDAO(getORMInterface(), univModel.getObject())
									.getActiveSemester()
									.getSemesterName()
									+	", " 
									+	univModel
										.getObject()
										.getCurrentAcadYear())
		);
		
		//If semester change is pending
		WebMarkupContainer wmcSCP = new WebMarkupContainer("semChangePending"){
			
			private static final long serialVersionUID = 870916036739383541L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_SCHEDULE_SEMESTER_CHANGE;
			}
		};
		add(wmcSCP);
		
		//If allot elective is pending
		WebMarkupContainer wmcEAP = new WebMarkupContainer("eleAllotPending"){
			
			private static final long serialVersionUID = -2105083232930192316L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_LOCK_ELECTIVE_SELECTION;
			}
		};
		add(wmcEAP);
	}

	public void renderHead(IHeaderResponse resp) {
		resp.renderOnDomReadyJavascript("" +
				"$('#"+confirmDialogue.getMarkupId()+"').dialog({"+
					    "autoOpen: false,"+
					    "width: 600,"+
					    "modal: true,"+
					    "buttons: {"+
					        "\"Ok\": function() {"+ 
					        "$(this).dialog(\"close\");"+
					        "$('#"+f.getMarkupId()+"').submit();"+
					         "return true; "+
					        "}, "+
					        "\"Cancel\": function() {"+ 
						    "$(this).dialog(\"close\");"+ 
					            "return false; "+
					        "} "+
					    "}"+
					"});"+
				"$('#chSemSubm').click(function(){$('#"+confirmDialogue.getMarkupId()+"').dialog('open');return false;});");
	}

}
